package misscl.online.slh_gym.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import misscl.online.slh_gym.entity.*;
import misscl.online.slh_gym.service.*;
import misscl.online.slh_gym.util.AjaxResult;
import misscl.online.slh_gym.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author theo
 * @since 2022/12/04 17:13
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private CoachCourseService coachCourseService;
    @Autowired
    private CoachService coachService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private AppointmentTimeService appointmentTimeService;
    @Autowired
    private AppointmentService appointmentService;

    @PutMapping("/")
    public AjaxResult put(@RequestBody AppointmentQo appointmentQo, HttpServletRequest request){
        //1.获取token，解析userid
        Integer uid = 0;
        try{
            String authorization = request.getHeader("authorization");
            uid = Integer.valueOf(JwtUtils.getClaimsByToken(authorization).getSubject());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("用户会话过期，请重新登录");
        }
        //2.通过教练id和课程id获取教练课程id ccid
        int courseId = appointmentQo.getCourseId();
        int coachId = appointmentQo.getCoachId();
        QueryWrapper<CoachCourse> queryWrapper = new QueryWrapper<>();
        QueryWrapper<CoachCourse> eq = queryWrapper.eq("course_id", courseId)
                .eq("coach_id", coachId);
        List<CoachCourse> list = coachCourseService.list(eq);
        if (list.size() == 0){
            return AjaxResult.error("暂无该课程");
        }
        //3.通过ccid，预约时间判断是否存在
        QueryWrapper<Appointment> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Appointment> eq1 = queryWrapper1.eq("cc_id", list.get(0).getId())
                .eq("appointment_date", appointmentQo.getAppointmentDate())
                .eq("at_id", appointmentQo.getAppointmentTimeId());
        List<Appointment> list1 = appointmentService.list(eq1);
        if (list1.size() > 0){
            //3.1 y 返回友好提示
            return AjaxResult.error("已被预约，请重新选择时间");
        }else{
            //3.2 n 添加数据，提示成功
            Appointment appointment = new Appointment();
            appointment.setAppointmentDate(appointmentQo.getAppointmentDate());
            appointment.setAtId(appointmentQo.getAppointmentTimeId());
            appointment.setCcId(list.get(0).getId());
            appointment.setUserId(uid);
            boolean save = appointmentService.save(appointment);
            //教练名
            Coach byId = coachService.getById(appointmentQo.getCoachId());
            //课程名
            Course byId1 = courseService.getById(appointmentQo.getCourseId());
            //时间段
            AppointmentTime byId2 = appointmentTimeService.getById(appointmentQo.getAppointmentTimeId());
            AppointmentVo appointmentVo = new AppointmentVo(byId1.getName(), byId.getName(), byId2.getStartTime(),appointment.getAppointmentDate());

            if (save){
                return AjaxResult.ok("预约成功").data("AppointmentVo",appointmentVo);
            }else {
                return AjaxResult.error("预约失败");
            }


        }
    }
    @GetMapping("/list")
    public AjaxResult list(HttpServletRequest request){
        Integer uid = 0;
        try{
            String authorization = request.getHeader("authorization");
            uid = Integer.valueOf(JwtUtils.getClaimsByToken(authorization).getSubject());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("用户会话过期，请重新登录");
        }
        QueryWrapper<Appointment> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Appointment> eq1 = queryWrapper1.eq("user_id", uid);
        List<Appointment> list = appointmentService.list(eq1);
        ArrayList<AppointmentVo> AppointmentVos = new ArrayList<>();
        for (Appointment appointment : list) {
            CoachCourse cc = coachCourseService.getById(appointment.getCcId());
            //教练名
            Coach byId = coachService.getById(cc.getCoachId());
            //课程名
            Course byId1 = courseService.getById(cc.getCourseId());
            //时间段
            AppointmentTime byId2 = appointmentTimeService.getById(appointment.getAtId());
            AppointmentVo appointmentVo = new AppointmentVo(
                    byId1.getName(),
                    byId.getName(),
                    byId2.getStartTime(),
                    appointment.getAppointmentDate()
            );

            AppointmentVos.add(appointmentVo);
        }
        return AjaxResult.ok("获取用户的预约列表成功").data("appointmentList",AppointmentVos);
    }
}
